<?php
namespace app\system\controller;

use app\common\model\CmsFieldModel;
use app\common\model\CmsMoudleModel;
use think\Request;

class CmsFlagController extends BaseController
{

    private $cmsMoudleModel;
    private $cmsMoudleCache;
    private $cmsFieldMoudle;
    private $mid;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->mid            = $this->request->param('mid/d', 1);
        $this->cmsMoudleCache = cache('cms_moudle');
        $this->cmsFieldMoudle = new CmsFieldModel();
        $this->cmsMoudleModel = new CmsMoudleModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $flagList = $this->cmsFieldMoudle->getFlaglist($this->mid);
        $this->assign('moudle', $this->cmsMoudleCache);
        $this->assign('flagList', $flagList);
        return $this->fetch();
    }

    /**
     * 添加字段
     * @return mixed
     */
    public function add()
    {
        $flagName = $this->request->param('flagName');
        if (empty($flagName)) {
            $this->error('请填写推荐位！');
        }
        $this->cmsFieldMoudle->addFlag($this->mid, $flagName);
        $this->success('添加成功！');
    }

    /**
     * 修改字段
     * @return mixed
     */
    public function edit()
    {
        $data = $this->request->param('flag/a');
        if ($this->cmsFieldMoudle->editFlag($this->mid, $data)) {
            $this->success('修改成功！');
        } else {
            $this->error($this->cmsFieldMoudle->getError());
        }
    }

    /**
     * 删除字段
     */
    public function del()
    {
        $index = $this->request->param('index');
        $this->cmsFieldMoudle->delFlag($this->mid, $index);
        $this->success('删除成功！');
    }

}
